package cn.zyq.ykb.service.tk.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.zyq.ykb.beans.tk.vo.TotalVo;
import cn.zyq.ykb.beans.user.User;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.enums.core.ResponseCodeEnum;
import cn.zyq.ykb.system.exception.BusinessException;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.tk.ChangeMapper;
import cn.zyq.ykb.beans.tk.Change;
import cn.zyq.ykb.service.tk.ChangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.UUID;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author SystemAuth
 * @date 2023-07-03
 */
@Service
public class ChangeServiceImpl  extends BaseService implements ChangeService {

    @Autowired
    private ChangeMapper changeMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param changeID 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public Change selectChangeById(Integer changeID) {
        Change change = changeMapper.selectByPrimaryKey(changeID);
        if (change.getDeleted() == 1) {
            return null;
        }
        return change;
    }

    /**
     * 查询【请填写功能名称】
     *
     * @param serviceID 业务ID
     * @return 【请填写功能名称】
     */
    @Override
    public Change selectChangeByServiceID(String serviceID) {
        Change param = new Change();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.changeMapper.selectOne(param);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param pageParam
     * @return 【请填写功能名称】
     */
    @Override
    public PageInfo<Change> selectChangeList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<Change> list = this.changeMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param pageParam
     * @return 【请填写功能名称】
     */
    @Override
    public List<Change> selectChangeAllList(PageParam pageParam) {
        return this.changeMapper.list(pageParam.getKv());
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param param 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertChange(Change param) {
        if (param.getState().equals(1)){
            param.setPrice(+param.getPrice());
        }else {
            param.setPrice(-param.getPrice());
        }
        this.setCreateBaseData(param, new User());
        return this.changeMapper.insertSelective(param);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param param 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateChange(Change param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.changeMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param changeIDs 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteChangeByIds(Integer changeIDs) {
        Change change = this.changeMapper.selectByPrimaryKey(changeIDs);
        if (BeanUtil.isEmpty(change)){
            throw new BusinessException(ResponseCodeEnum.ERROR,"当前数据不存在");
        }
        int i = this.changeMapper.deleteByPrimaryKey(changeIDs);
        if (i == 0){
            throw new BusinessException(ResponseCodeEnum.ERROR,"删除失败");
        }
        return 1;
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param changeID 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteChangeById(Integer changeID) {
        Change change = this.selectChangeById(changeID);
        if (change == null) {
            return 0;
        } else {
            Change storeInfo = new Change();
            storeInfo.setId(changeID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.changeMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }

    @Override
    public TotalVo total(Integer tUserID) {
        Integer ysrTotal = this.changeMapper.ysrTotal(tUserID);
        Integer yzcTotal = this.changeMapper.yzcTotal(tUserID);
        Integer total =this.changeMapper.total(tUserID);
        TotalVo vo = new TotalVo();
        vo.setTotal(total);
        vo.setYsrTotal(ysrTotal);
        vo.setYzcTotal(yzcTotal);
        return vo;
    }

    @Override
    public int batchRemove() {
        Change change = new Change();
        change.setDeleted(0);
        List<Change> select = this.changeMapper.select(change);
        List<Integer> collect = select.stream().map(p -> p.getId()).collect(Collectors.toList());
        for (Integer id : collect) {
            int i = this.deleteChangeByIds(id);
            if (i == 0){
                throw new BusinessException(ResponseCodeEnum.ERROR,"删除id:"+id+"失败");
            }
        }
        return 1;
    }
}
